<?php

namespace api\admin;

//活动类
use utils\zip;

class active
{
    //添加
    public function add($data){
        $name=$data['name'];
        $remark=$data['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"insert into active(name,remark,create_time,update_time) values('$name','$remark',$time,$time)");

        mysqli_close($con);
        if($res){
            return out_right('添加成功！');
        }else{
            return out_error('添加失败！');
        }
    }

    //修改
    public function edit($data){
        $id=(int)$data['id'];
        $time=time();
        $name=$data['name'];
        $remark=$data['remark'];

        $con=con();
        $res=mysqli_query($con,"update active set name='$name',remark='$remark',update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right('修改成功！');
        }else{
            return out_error('修改失败！');
        }
    }

    //删除
    public function del($data){
        $id=(int)$data['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update active set is_delete=0,delete_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right('删除成功！');
        }else{
            return out_error('删除失败！');
        }
    }

    //获取分页列表
    public function getList($data){
        $name=$data['name'];
        $page=(int)$data['page'];
        $size=(int)$data['size'];
        $start=($page-1)*$size;

        $con=con();
        $where=" where is_delete=1 ";
        if(!empty($name)){
            $where.=" and name like '%$name%'";
        }
        $limit=" order by `id` desc limit $start,$size";
        $sql="select * from active $where";
        $data=mysqli_query($con,$sql.$limit);
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        mysqli_close($con);
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    //修改状态
    public function changeStatus($request){
        $id=(int)$request['id'];
        $status=(int)$request['status'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update active set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }


}